package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

type job struct {
	value int64
}

type result struct {
	*job
	sum int64
}

var jobChan = make(chan *job, 100)
var resultChan = make(chan *result, 100)

var wg sync.WaitGroup

func main() {
	wg.Add(1)
	go random(jobChan)

	for i := 0; i < 24; i++ {
		go sum(jobChan, resultChan)
	}

	for {
		v, ok := <-resultChan
		if !ok {
			break
		}
		fmt.Print(v.job.value, v.sum)
		//fmt.Sprintf("job int64:%v,sum int64:%v\n", v.job.value, v.sum)
	}
}

func random(ch chan<- *job) {
	rand.Seed(time.Now().UnixNano())
	for {
		tmp := &job{
			value: rand.Int63(),
		}
		ch <- tmp
		time.Sleep(time.Millisecond * 1000)
	}
}

func sum(ch <-chan *job, ch1 chan<- *result) {
	select {
	case x := <-ch:
		sum := int64(0)
		n := x.value
		for n > 0 {
			sum += n % 10
			n /= 10
		}
		tmp := &result{
			job: x,
			sum: sum,
		}
		ch1 <- tmp
	}
}
